package com.mccraywebtech.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.mccraywebtech.dao.GenericDAO;
import com.mccraywebtech.models.User;

@Repository("userMockDAO")
public class UserMockDAOImpl implements GenericDAO<User> {
	private List<User> users = new ArrayList<User>();
	private int count = 0;

	public UserMockDAOImpl() {
		User admin = new User();
		admin.setAdmin(true);
		admin.setPassword("admin");
		;
		admin.setUsername("admin");
		;
		admin.setId(0);
		users.add(admin);
		count++;
	}

	@Override
	public void saveObject(User t) {
		t.setId(count);
		count++;
		users.add(t.getId(), t);

	}

	@Override
	public void updateObject(User c) {
		users.add(c.getId(), c);
	}

	@Override
	public void deleteObject(int id) {
		users.remove(id);
	}

	@Override
	public User getObject(int id) {
		return users.get(id);
	}

	public User getByUsername(String username) {
		for (User user : users) {
			if (user.getUsername().equals(username)) {
				return user;
			}
		}
		return null;
	}

	@Override
	public List<User> getAllObjects() {
		return users;
	}

}
